﻿package mortal.game.view.mainUI.shortcutbar
{
    import Message.Game.*;
    import com.gengine.core.frame.*;
    import com.gengine.global.*;
    import extend.language.*;
    import flash.events.*;
    import flash.utils.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.scene3D.ai.*;
    import mortal.game.scene3D.ai.ais.*;
    import mortal.game.scene3D.ai.data.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.cd.*;
    import mortal.game.view.copy.petBreak.*;
    import mortal.game.view.skill.*;
    import mortal.game.view.systemSetting.*;
    import mortal.mvc.core.*;

    public class ShortcutBarKeyProcessor extends Object
    {
        private var _timer:FrameTimer;
        private var downKeys:Array;
        private var _nextCanUseShortcutTime:int = 0;
        private var _mouseDownUseComboSkill:Boolean = false;
        private var _lastPickupTime:int = 0;

        public function ShortcutBarKeyProcessor()
        {
            this.downKeys = [];
            this._timer = new FrameTimer(1);
            this._timer.isRepair = false;
            this._timer.addListener(TimerType.ENTERFRAME, this.onEnterFrameHandler);
            Global.stage.addEventListener(Event.DEACTIVATE, this.onDeactivateHandler, false, 99999);
            return;
        }// end function

        private function onDeactivateHandler(event:Event) : void
        {
            this.downKeys = [];
            this._nextCanUseShortcutTime = 0;
            this._mouseDownUseComboSkill = false;
            this.removeListener();
            return;
        }// end function

        public function isWorking() : Boolean
        {
            return this._timer.running;
        }// end function

        public function set mouseDownUseCombat(param1:Boolean) : void
        {
            if (param1 == this._mouseDownUseComboSkill)
            {
                return;
            }
            this._mouseDownUseComboSkill = param1;
            if (this.downKeys.length == 0)
            {
                if (this._mouseDownUseComboSkill)
                {
                    this.addListener();
                }
                else
                {
                    this.removeListener();
                }
            }
            return;
        }// end function

        private function addListener() : void
        {
            this._timer.start();
            return;
        }// end function

        private function removeListener() : void
        {
            this._timer.stop();
            return;
        }// end function

        private function onEnterFrameHandler(param1 = null) : void
        {
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:SkillInfo = null;
            var _loc_2:* = this.downKeys.length;
            while (_loc_2 >= 0)
            {
                
                _loc_3 = this.downKeys[_loc_2];
                _loc_4 = FrameManager.flashFrame.getTime();
                if (_loc_3 == GameConst.PickupKeyCode && _loc_4 - this._lastPickupTime >= GameConst.PickUpTimeDistance)
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.KeyPickDropItem));
                    this._lastPickupTime = _loc_4;
                    return;
                }
                if (this.checkAndUseKey(_loc_3))
                {
                    return;
                }
                _loc_2 = _loc_2 - 1;
            }
            if (this._mouseDownUseComboSkill)
            {
                if (GameMapUtil.curMapState.isPetBreakMap)
                {
                    _loc_5 = PetBreakCopyUtil.firstSkill;
                }
                else
                {
                    _loc_5 = Cache.instance.skill.getComboSkill();
                }
                if (_loc_5 != null)
                {
                    this.checkAndUseSource(_loc_5);
                }
            }
            return;
        }// end function

        public function addKeyDown(param1:int) : void
        {
            var _loc_2:* = this.downKeys.indexOf(param1);
            if (_loc_2 >= 0)
            {
                this.downKeys.splice(_loc_2, 1);
            }
            this.downKeys.push(param1);
            if (this.downKeys.length == 1 && !this._mouseDownUseComboSkill)
            {
                this.addListener();
            }
            var _loc_3:* = this.checkAndUseKey(param1, true, true);
            return;
        }// end function

        public function delKeyDown(param1:int) : void
        {
            var _loc_2:* = this.downKeys.indexOf(param1);
            if (_loc_2 >= 0)
            {
                this.downKeys.splice(_loc_2, 1);
            }
            else if (param1 < 10)
            {
                _loc_2 = this.downKeys.indexOf(param1 + 10);
                if (_loc_2 >= 0)
                {
                    this.downKeys.splice(_loc_2, 1);
                }
            }
            else if (param1 > 10)
            {
                _loc_2 = this.downKeys.indexOf(param1 - 10);
                if (_loc_2 >= 0)
                {
                    this.downKeys.splice(_loc_2, 1);
                }
            }
            if (!this._mouseDownUseComboSkill && this.downKeys.length == 0)
            {
                this.removeListener();
            }
            return;
        }// end function

        public function checkAndUseKey(param1:int, param2:Boolean = false, param3:Boolean = false) : Boolean
        {
            var _loc_4:Object = null;
            var _loc_5:SkillInfo = null;
            if (param1 < 0)
            {
                return false;
            }
            if (Cache.instance.skill.isShowingExSkill)
            {
                _loc_4 = GameController.shortcut.exSkillBar.getDragSource(param1);
            }
            else
            {
                _loc_4 = GameController.shortcut.shortcutBar.getDragSource(param1);
                if (!param3 && _loc_4 != null)
                {
                    _loc_5 = _loc_4 as SkillInfo;
                    if (_loc_5 == null)
                    {
                        return false;
                    }
                    if (_loc_5.skillId != GameConst.RollingSkillId && _loc_5.skillId != GameConst.JPSkillId && _loc_5.skillId != GameConst.SkillMount && _loc_5.skillId != Cache.instance.wizard.XpSkillId)
                    {
                        if (_loc_5.position <= 0 || _loc_5.position > 20)
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return true;
                    }
                }
            }
            if (_loc_4 == null)
            {
                return false;
            }
            return this.checkAndUseSource(_loc_4, param2);
        }// end function

        public function checkAndUseSource(param1:Object, param2:Boolean = false) : Boolean
        {
            var _loc_7:Array = null;
            var _loc_8:String = null;
            var _loc_9:SkillInfo = null;
            var _loc_10:ItemData = null;
            var _loc_11:SPet = null;
            var _loc_12:AutoFightAI = null;
            var _loc_13:PetAutoFightAI = null;
            var _loc_3:* = ShortcutBarUtil.parseType(param1);
            var _loc_4:* = Cache.instance.cd.getCDData(param1);
            var _loc_5:* = ClientSetting.local.getIsDone(IsDoneType.IsNotCD);
            if (!_loc_5 && _loc_4 && _loc_4.isCoolDown)
            {
                if (param2)
                {
                    _loc_8 = Language.getString(20052);
                    if (_loc_3 == CDDataType.itemData)
                    {
                        _loc_8 = Language.getString(20053);
                    }
                    _loc_8 = Language.getStringByParam(20051, _loc_8);
                    MsgManager.showFightTipsImpl(_loc_8, MsgRollTipsType.msgRollTips6, true);
                }
                return false;
            }
            var _loc_6:* = getTimer();
            if (_loc_6 < this._nextCanUseShortcutTime)
            {
                return false;
            }
            this._nextCanUseShortcutTime = _loc_6 + 75;
            if (!this.canIUseKeySource(param1))
            {
                return false;
            }
            switch(_loc_3)
            {
                case CDDataType.skillInfo:
                {
                    _loc_9 = param1 as SkillInfo;
                    if (AutoFightAI.isWorking && AIManager.curAI is AutoFightAI)
                    {
                        _loc_12 = AIManager.curAI as AutoFightAI;
                        _loc_12.useSkill(_loc_9);
                        return true;
                    }
                    if (PetAutoFightAI.isWorking)
                    {
                        _loc_13 = AIManager.curAI as PetAutoFightAI;
                        _loc_13.useSkill(_loc_9);
                        return true;
                    }
                    if (AIManager.curAI is FollowFightBossToDeadAI)
                    {
                        (AIManager.curAI as FollowFightBossToDeadAI).useSkill(_loc_9);
                        return true;
                    }
                    if (AIManager.curAI is FollowFightAI && AIManager.curAI.data != null && (AIManager.curAI.data as FollowFightAIData).skillInfo.skillId == _loc_9.skillId)
                    {
                        return false;
                    }
                    return GameController.skill.checkAndStartAIBySkill(_loc_9, param2);
                }
                case CDDataType.itemData:
                {
                    _loc_10 = param1 as ItemData;
                    if (_loc_10 != null)
                    {
                        _loc_7 = Cache.instance.pack.backPackCache.getItemsByCode(_loc_10.itemInfo.code);
                        if (_loc_7 == null || _loc_7.length == 0)
                        {
                            return false;
                        }
                        _loc_7.sort(this.sortItemData);
                        _loc_10 = _loc_7[0];
                        if (_loc_10 == null || _loc_10.serverData.itemAmount <= 0)
                        {
                            return false;
                        }
                        Dispatcher.dispatchEvent(new DataEvent(EventName.BackPack_Use, _loc_10));
                    }
                    break;
                }
                case CDDataType.petInfo:
                {
                    _loc_11 = param1 as SPet;
                    Dispatcher.dispatchEvent(new DataEvent(EventName.PetChangeState, _loc_11));
                    return true;
                }
                default:
                {
                    break;
                }
            }
            return false;
        }// end function

        private function canIUseKeySource(param1:Object) : Boolean
        {
            if (param1 is SkillInfo)
            {
                if (BuffUtil.isCanRoleFireSkill(param1 as SkillInfo))
                {
                    return true;
                }
                return false;
            }
            return true;
        }// end function

        private function sortItemData(param1:ItemData, param2:ItemData) : int
        {
            var _loc_3:* = ItemsUtil.isBind(param1) ? (1) : (0);
            var _loc_4:* = ItemsUtil.isBind(param2) ? (1) : (0);
            if (_loc_3 > _loc_4)
            {
                return -1;
            }
            if (_loc_3 < _loc_4)
            {
                return 1;
            }
            if (param1.serverData.itemAmount < param2.serverData.itemAmount)
            {
                return -1;
            }
            return 1;
        }// end function

    }
}
